<template>
  <div class="auth-container">
    <el-card class="auth-card" shadow="hover">
      <div class="auth-header">
        <h2>用户登录</h2>
        <p>欢迎回到中医智能健康助手</p>
      </div>
      <el-form ref="formRef" :model="form" :rules="rules" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" autocomplete="username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            show-password
            autocomplete="current-password"
          />
        </el-form-item>
        <el-button type="primary" class="auth-submit" :loading="loading" @click="handleSubmit">
          登 录
        </el-button>
      </el-form>
      <div class="auth-footer">
        还没有账号？
        <router-link to="/register">立即注册</router-link>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';
import { loginApi } from '@/api/auth';
import { useAuthStore } from '@/store/modules/auth';
import type { LoginRequest } from '@/types/auth';

const router = useRouter();
const route = useRoute();
const authStore = useAuthStore();

const formRef = ref<FormInstance>();
const loading = ref(false);

const form = reactive<LoginRequest>({
  username: '',
  password: '',
});

const rules: FormRules<LoginRequest> = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
};

const handleSubmit = () => {
  formRef.value?.validate(async (valid) => {
    if (!valid) return;
    loading.value = true;
    try {
      const data = await loginApi(form);
      authStore.setAuthResponse(data);
      ElMessage.success('登录成功，欢迎回来！');
      const redirectPath = (route.query.redirect as string) || '/dashboard';
      router.replace(redirectPath);
    } finally {
      loading.value = false;
    }
  });
};
</script>

<style scoped lang="scss">
.auth-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #d7e3fc 0%, #f5f7fa 100%);
}

.auth-card {
  width: 420px;
  padding: 1.5rem 1.75rem 2rem;
  border-radius: 16px;
}

.auth-header {
  text-align: center;
  margin-bottom: 1.5rem;

  h2 {
    margin: 0;
    font-size: 1.6rem;
    font-weight: 600;
    color: #1f2937;
  }

  p {
    margin: 0.5rem 0 0;
    color: #6b7280;
    font-size: 0.95rem;
  }
}

.auth-submit {
  width: 100%;
  margin-top: 0.5rem;
  height: 44px;
  font-size: 1rem;
  letter-spacing: 0.1em;
}

.auth-footer {
  margin-top: 1rem;
  text-align: center;
  color: #6b7280;

  a {
    margin-left: 0.25rem;
    color: #2563eb;
    font-weight: 500;
  }
}
</style>

